#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h> 

void* Consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while (true)
    {
        // sleep(2);
        // 1.获取数据/任务
        // int data = 0;
        Task t;
        bq->Pop(&t);
        // 2.处理数据
        // t.Excute();
        t();
        // std::cout << "Consumer ->" << t.result() << std::endl;
    }
}

void* Productor(void* args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while (true)
    {
        // 1.构建数据/任务
        // int data = rand() % 10 + 1; // [1, 10]
        // int x = rand() % 10 + 1;    // [1, 10]
        // usleep(x * 1000);
        // int y = rand() % 10 + 1;    // [1, 10]
        // Task t(x, y);
        // 2.生产数据
        bq->Equeue(DownLoad);
        std::cout << "Productor ->" << "DownLoad" << std::endl;
        sleep(1);
    }
}

int main()
{
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c1, c2, c3, p1, p2;
    pthread_create(&c1, nullptr, Consumer, (void*)bq);
    pthread_create(&c2, nullptr, Consumer, (void*)bq);
    pthread_create(&c3, nullptr, Consumer, (void*)bq);
    pthread_create(&p1, nullptr, Productor, (void*)bq);
    pthread_create(&p2, nullptr, Productor, (void*)bq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);

    return 0;
}